import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { InnerMineEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { ActionUtils } from "../utils/ActionUtil";
import { EActionCode } from "framework/FSM/Action/ActionCode";

 

/**
 * 采集任务
 */ 
/*@profile*/
export class HarvestTask<T extends IStateArg<Creep>>  extends State<T>
{

    //

    public  getActions(arg:T):Action[]
    {
       //global.Log.Info(`${arg.entity.name} 采集任务`);
        const creep = arg.entity;
        if(!creep.memory.taskid)
        {

            return []
        }
        const task =arg.task as ITask<ICreepTaskData<"harvest">>;
        //const task = arg.entity.ownerRoom().findTaskById<ITask<ICreepTaskData<"harvest">>>(creep.memory.taskid,"creep");
        if(!task)
        {
            //global.Log.Info(`${arg.entity.name} 采集任务 1`);
            return []
        }
        const bindMine = arg.entity.getEntityByID<InnerMineEntity>(task.data.arg.entityId);
        const memorysites = bindMine.getNodeMemory().sites;
        if(!memorysites?.length)
        {
            // 有建筑工地，决不后退
            const actions = ActionUtils.getEscapeSourceKeeperAction(creep,bindMine.BasePosition);
            if(actions)
            {
                return actions
            }
        }
        

       // global.Log.Info(`${arg.entity.name} -采集2`)
        
       
       
        const repobjects = bindMine.getNodeMemory().repairObjects;
        let transferObj 
        if(creep.store.getUsedCapacity()==0)
        {
            creep.memory.state = 0;

        }
        else  if(creep.store.getFreeCapacity()==0|| creep.store.getFreeCapacity()<creep.memory.body["work"]*2)
        {
           
            if(memorysites?.length || repobjects?.length)
            {
                creep.memory.state = 1;
            }
            
            transferObj =   bindMine.getTransferObject();
            if(transferObj)
            {
                creep.memory.state =1;
            }
        }
        if( creep.memory.state )
        {
            // 工作 建设。或者修理.或者transfer
            if(memorysites&& memorysites.length)
            {
                return [ActionArgHelper.actionBuild(creep,Game.getObjectById(memorysites[0]) as ConstructionSite)]
            }
            // 有需要修理的
           
            if(repobjects && repobjects.length>0)
            {
                const container = Game.getObjectById(repobjects[0]);
                if(container)
                {
                    return [ActionArgHelper.actionRepair(creep,container as AnyOwnedStructure)]
                }
            }
            const obj = bindMine.getTransferObject();
            if(obj)
            {
                return [ActionArgHelper.actionTransfer(creep, obj,[{
                    resourceType:RESOURCE_ENERGY
                }])]
            }
            // 没事干就转为状态0
            if(!bindMine.Link)
            {
                creep.memory.state =0;
            }
           
        }
        if(creep.room.name == bindMine.roomName)
        {
            // 采集
            const container = bindMine.getContainer();
            if(container && !container.pos.isEqualTo(creep.pos) && !container.pos.lookMyCreep() )
            {
                creep.say(`${container.pos.x}_${container.pos.y}:${0}`) 
                return [
                    ActionArgHelper.warp(
                    ActionArgHelper.actionMoveto(creep,{pos:container.pos,range:0},{}),
                undefined,
                (arg)=>
                {
                    if(ActionUtils.isFlee(arg.arg.entity as Creep)!==0)
                    {
                        return true;
                    }
                    return false}
                )]
            }
            if(memorysites?.length )
            {
                // 有site 优先拾取或者直接withdraw
                const res = bindMine.getPickUpItem(RESOURCE_ENERGY);
                if(res.length)
                {
                    return [ActionArgHelper.actionPickup(creep,res[0])]
                }
                const container = bindMine.getContainer();
                if(container&& container.store.getUsedCapacity(RESOURCE_ENERGY))
                {
                    return [ActionArgHelper.actionWithdraw(creep,container,[{resourceType:RESOURCE_ENERGY}])]
                }
            }
            
            if(creep.room.getType() == 6)
            {
                return [ActionArgHelper.warp(ActionArgHelper.actionharvestEnergy(creep,bindMine.GUID()),undefined,(arg)=>
                    {
                        
                        if(ActionUtils.isFlee(arg.arg.entity))
                        {
                            // 已经过期了
                            return true;
                        }
                        const container = bindMine.getContainer();
                        if(container && !container.pos.isEqualTo(arg.arg.entity.pos))
                        {
                            return true;
                        }
                        return false
                    })]
            }
            else 
            {
                return [ActionArgHelper.actionharvestEnergy(creep,bindMine.GUID())]
            }
            
        }
        else
        {
           creep.say(`${bindMine.BasePosition.x}_${bindMine.BasePosition.y}:${1}`) 
           return [ActionArgHelper.actionMoveto(creep,{pos:bindMine.BasePosition,range:1},{})]
        }
 
    }
}
